<?php
class Model_Transactions extends MVC_MODEL {
	/**
	 * @var object SQL
	 */
	public $oDb_Transactions;
//	private $aData = array();
	
	public function __construct(){
		parent::__construct();
		$this->oDb_Transactions = new Db_Transactions();
	}
	public function setDataTransactions($fileUpload){
		$this->setData('packet_name',$_REQUEST[packet_name]);
		$this->setData('packet_description',$_REQUEST[packet_description]);
		$this->setData('packet_image',$fileUpload);
		$this->setData('packet_download_items',$_REQUEST[packet_download_items]);
		$this->setData('packet_period',$_REQUEST[packet_period]);
		$this->setData('packet_price',$_REQUEST[packet_price]);
		$this->setData('packet_status',$_REQUEST[packet_status]);
		return $this->aData;
	}
	public function setDataUserAccount($user_account){
//		var_dump($user_account);die();
		$this->setData('user_id',$user_account[user_id]);
		$this->setData('can_be_downloaded_items',$user_account[download_items]);
		$this->setData('downloaded_items','0');
		$time = time();
		$this->setData('start_date',date('Y-m-d',$time));
		$this->setData('end_date',date('Y-m-d',$time+($user_account[periods]*24*3600)));
		return $this->aData;
	}
	public function setDataUpdateUserAccount($user_account){
//		var_dump($user_account);die();
		$arr_trans = $this->getTransactionsById($_REQUEST[id]);
		$this->setData('can_be_downloaded_items',$user_account[can_be_downloaded_items] + $arr_trans->fields[download_items]);
		$this->setData('downloaded_items',$user_account[downloaded_items]);
		if($user_account[start_date] <= date("Y-m-d")){
			$this->setData('start_date',date("Y-m-d"));
		}
		else{
			$this->setData('start_date',$user_account[start_date]);
		}
		if($user_account[end_date]<=date("Y-m-d"))
			$this->setData('end_date',date('Y-m-d',$time+($arr_trans->fields[periods]*24*3600)));
		else
			$this->setData('end_date',date('Y-m-d',strtotime($user_account[end_date])+($arr_trans->fields[periods]*24*3600)));
		return $this->aData;
	}
	public function setDataUpdateUserAccountReject($user_account){
//		var_dump($user_account);die();
		$arr_trans = $this->getTransactionsById($_REQUEST[id]);
		$this->setData('can_be_downloaded_items',$user_account[can_be_downloaded_items] - $arr_trans->fields[download_items]);
		$this->setData('downloaded_items',$user_account[downloaded_items]);
		/*if($user_account[start_date] <= date("Y-m-d")){
			$this->setData('start_date',date("Y-m-d"));
		}
		else{
			$this->setData('start_date',$user_account[start_date]);
		}*/
		/*if($user_account[end_date]<=date("Y-m-d"))
			$this->setData('end_date',date('Y-m-d',$time+($arr_trans->fields[periods]*24*3600)));
		else
			$this->setData('end_date',date('Y-m-d',strtotime($user_account[end_date])+($arr_trans->fields[periods]*24*3600)));*/
		$this->setData('start_date',$user_account[start_date]);
		$this->setData('end_date',date('Y-m-d',strtotime($user_account[end_date])-($arr_trans->fields[periods]*24*3600)));
		return $this->aData;
	}
	public function insertTransactions($fileUpload){
		$aData = $this->setDataTransactions($fileUpload);
		 $this->oDb_Transactions->doInsertTransactions($aData);
	}
	public function listTransactions(){
		$this->oDb_Transactions->doListTransactions();
		return $this->oDb_Transactions;
	}
	public function deleteTransactionsById($id){
		$this->oDb_Transactions->doDeleteTransactionsById($id);
//		return $this->oDb_Transactions;
	}
	public function getTransactionsById($id){
		return $this->oDb_Transactions->doGetTransactionsById($id);
	}
	public function updateTransactionsByID($date_time,$status,$id){
		$this->oDb_Transactions->doUpdateTransactionsById($date_time,$status,$id);
	}
	public function checkExistUserId($user_id){
		return $this->oDb_Transactions->docheckExistUserId($user_id);
	}
	public function insertUserAccount($user_id){
		$arr_user_account = $this->checkExistUserId($user_id);
//		var_dump($arr_user_account);die();
		if(!$arr_user_account->fields){
			$arr_user_account = $this->getTransactionsById($_REQUEST[id]);
//			var_dump($arr_user_account->fields);die();
			$aData = $this->setDataUserAccount($arr_user_account->fields);
			$this->oDb_Transactions->doInsertUserAccount($aData);
		}else{
			$aData = $this->setDataUpdateUserAccount($arr_user_account->fields);
			$this->oDb_Transactions->doUpdateUserAccountById($aData,$user_id);
		}
	}
	public function updateUserAccountReject($user_id){
		$arr_user_account = $this->checkExistUserId($user_id);
		if($arr_user_account->fields){
			$aData = $this->setDataUpdateUserAccountReject($arr_user_account->fields);
			$this->oDb_Transactions->doUpdateUserAccountById($aData,$user_id);
		}
	}
}
?>
	